from dao.AccountDao import Function
from model.Account import Account
from util.DbUtils import Tool

f = Function()
t = Tool()


class AccountServices(object):

    # 注册
    def reg(self):
        uname = f.input_uname()
        while 1:
            upwd = input("请输入密码:")
            upwd2 = input("请再次输入密码:")
            if upwd != upwd2:
                continue
            else:
                break
        f.reg_change(uname, upwd)
        from app.Main import start_menu
        start_menu()

    # 登录
    def login(self):
        uname = input("请输入用户名:")
        index = f.check_uname(uname)
        if index >= 0:
            upwd = input("请输入密码")
            global user_index, user_name, a
            a = Account(uname)
            user_index = index
            user_name = a.name
            print(a.pwd)
            if a.pwd == upwd:
                print("登录成功!")
                from app.Main import main_menu
                main_menu()
            else:
                print("密码错误,请重试")
        else:
            print("用户名不存在")
        from app.Main import start_menu
        start_menu()

    # 查询
    def query(self):
        user_list = t.select("select * from userinfo")
        print(user_list[user_index][2])
        from app.Main import main_menu
        main_menu()

    # 取款
    def withdraw(self):
        from app.Main import main_menu
        money = int(input("请输入取款金额"))
        if money < 0:
            print("请输入合法金额")
            self.withdraw()
        elif f.check_money(money, user_index):
            f.money_change(user_index, -money)
            from app.Main import main_menu
            main_menu()
        else:
            print("余额不足,请查询")
            main_menu()

    # 存款
    def deposite(self):
        money = int(input("请输入存款金额"))
        if money < 0:
            print("请输入合法金额")
            from app.Main import main_menu
            main_menu()
        else:
            f.money_change(user_index, money)
            from app.Main import main_menu
            main_menu()

    # 转账
    def transer(self):
        target_name = input("请输入目标账户名")
        if target_name == user_name:
            print("不可以以自己为转账目标,请重试")
            self.transer()
        else:
            target_index = f.check_uname(target_name)
            if target_index >= 0:
                money = int(input("请输入转账金额"))
                if money > 0:
                    if f.check_money(money, user_index):
                        f.money_change(user_index, -money)
                        f.money_change(target_index, money)
                        from app.Main import main_menu
                        main_menu()
                    else:
                        print("余额不足,请充值")
                        self.deposite()
                else:
                    print("请输入合法金额")
                    self.transer()
            else:
                print("用户名不存在,请重试")
                self.transer()

    # 流水
    def history(self):
        print("哗---哗---哗---")
        lst = t.select("select * from waterrun where uname = '{}'".format(user_name))
        print(lst)
        from app.Main import main_menu
        main_menu()
